package com.zdsoft.blp.zongbuju.shouye.taoshihui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter.AddressAdapter;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.Address;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/9/9.
 */
public class GuanLiAddressActivity extends BaseActivity implements NetCallback{
    private FanHuiView fh;
    private TextView tv_biaoti;
    private ListView lv;
    private List<Address> list ;
    private AddressAdapter adapter;
    private static final int HUOQUADDRESS =1;
    private static final int GUANLI =2;
    private RelativeLayout rl_guanLiAddress;
    @Override
    protected void init() {
        setContentView(R.layout.xuanzeaddresslayout);
        fh = find(R.id.fh);
        tv_biaoti = find(R.id.tv_biaoti);
        lv = find(R.id.lv);
        rl_guanLiAddress = find(R.id.rl_guanLiAddress);

        fh.getTv_title().setText("管理收货地址");
        tv_biaoti.setText("新增地址");
        getAddress();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GuanLiAddressActivity.this,XiuGaiAddressActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("address", list.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        rl_guanLiAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GuanLiAddressActivity.this,SHAddressActivity.class);
                intent.putExtra("isFirst",false);
                startActivity(intent);
            }
        });
    }

    public void getAddress() {
        SharedPreferences sp = GuanLiAddressActivity.this.getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId", "");
        Net net = new Net();
        net.url = API.ADDRESS_URL;
        net.nameSpace = API.ADDRESS_NAMESPACE;
        net.methodName = API.ADDRESSLIST;
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        net.parms = map;
        net.code = HUOQUADDRESS;
        getServer(net, GuanLiAddressActivity.this);
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag&&code==HUOQUADDRESS){
            try {
                JSONArray array = new JSONArray(res);
                if(array.length()>0){
                    list = new ArrayList<>();
                    for(int i = 0;i<array.length();i++){
                        JSONObject address = array.getJSONObject(i);
                        list.add(new Address(address.getString("id"),address.getString("xingming"),
                                address.getString("moren"),address.getString("qu"),address.getString("xiangxi"),address.getString("dianhua")));
                    }
                    adapter =new AddressAdapter(list,getApplicationContext(),GUANLI);
                    lv.setAdapter(adapter);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getAddress();
    }
}
